package com.benefm.multipar.pad.ble;

import android.util.Log;

import com.benefm.multipar.pad.BaseApp;
import com.benefm.multipar.pad.event.AtmosphericPressure;
import com.benefm.multipar.pad.event.CarbonDioxideEvent;
import com.benefm.multipar.pad.event.ChargeInfoEvent;
import com.benefm.multipar.pad.event.EventAction;
import com.benefm.multipar.pad.event.FileMessageEvent;
import com.benefm.multipar.pad.event.FileNameEvent;
import com.benefm.multipar.pad.event.GyroscopeInfoEvent;
import com.benefm.multipar.pad.event.HolterModeEvent;
import com.benefm.multipar.pad.event.HolterSetEvent;
import com.benefm.multipar.pad.event.HrBatteryEvent;
import com.benefm.multipar.pad.event.LatLngEvent;
import com.benefm.multipar.pad.event.LeadInfoEvent;
import com.benefm.multipar.pad.event.MsgEvent;
import com.benefm.multipar.pad.event.RespInfoEvent;
import com.benefm.multipar.pad.event.VersionInfoEvent;
import com.benefm.multipar.pad.util.StringUtil;
import com.benefm.multipar.pad.util.filter.EcgDataContext;
import com.clj.fastble.utils.HexUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BleHandleThread implements Runnable {

    private byte[] value;

    private String deviceName;

    public BleHandleThread(byte[] data) {
        this.value = data;
        deviceName = BLEManager.getInstance().getBleDevice().getName();
    }

    @Override
    public synchronized void run() {
        for (int i = 0; i < value.length; i++) {
            if (BLEManager.head1Byte != null && BLEManager.head2Byte != null
                    && BLEManager.head3Byte != null && BLEManager.head4Byte != null) {
                if (BLEManager.tempDataCount == 0) {
                    BLEManager.allDataCount = (BLEManager.head4Byte & 0xff) + 1;
                    BLEManager.onePack = new byte[BLEManager.headNub + BLEManager.allDataCount];
                    BLEManager.onePack[0] = BLEManager.HEAD_1;
                    BLEManager.onePack[1] = BLEManager.HEAD_2;
                    BLEManager.onePack[2] = BLEManager.head3Byte;
                    BLEManager.onePack[3] = BLEManager.head4Byte;
                }
                int resumeCount = BLEManager.allDataCount - BLEManager.tempDataCount;
                if (value.length - i < resumeCount) {
                    System.arraycopy(value, i, BLEManager.onePack, BLEManager.headNub + BLEManager.tempDataCount, value.length - i);
                    BLEManager.tempDataCount += value.length - i;
                    break;
                } else {
                    System.arraycopy(value, i, BLEManager.onePack, BLEManager.headNub + BLEManager.tempDataCount, resumeCount);
                    i += (resumeCount - 1);
                    BLEManager.tempDataCount += resumeCount;
                    if (BLEManager.tempDataCount == BLEManager.allDataCount) {
                        BLEManager.tempDataCount = 0;
                        BLEManager.allDataCount = 0;
                        BLEManager.head1Byte = null;
                        BLEManager.head2Byte = null;
                        BLEManager.head3Byte = null;
                        BLEManager.head4Byte = null;
                        handleData(BLEManager.onePack);
                    }
                    continue;
                }
            }
            if (BLEManager.head1Byte == null && value[i] == BLEManager.HEAD_1) {
                BLEManager.head1Byte = BLEManager.HEAD_1;
                continue;
            }

            if (BLEManager.head2Byte == null && BLEManager.head1Byte != null && value[i] != BLEManager.HEAD_2) {
                BLEManager.head1Byte = null;
                continue;
            }

            if (BLEManager.head2Byte == null && BLEManager.head1Byte != null && value[i] == BLEManager.HEAD_2) {
                BLEManager.head2Byte = BLEManager.HEAD_2;
                continue;
            }

            if (BLEManager.head3Byte == null && BLEManager.head2Byte != null) {
                BLEManager.head3Byte = value[i];
                continue;
            }

            if (BLEManager.head3Byte != null) {
                BLEManager.head4Byte = value[i];
            }

        }
    }

    private synchronized void handleData(byte[] value) {//value is onePack
        //校验数据的完整性
        String mValue = StringUtil.toHexString(value, " ");
        if (value[value.length - 1] != StringUtil.calculateXor(value))
            return;
        if ((value[2] & 0xff) <= 0x08) {
            if (BaseApp.isDebug)
                Log.v("lsy", "received data == " + mValue);
        } else {
            if (BaseApp.isDebug)
                Log.i("lsy", "received data == " + mValue);
        }
        //aa aa 01 1f 18
        //心电数据
        if ((value[2] & 0xff) == 0x01 || (value[2] & 0xff) == 0x08) {
            //解析波形数据
            int[] dataArray = StringUtil.changeNumber(value);
            Map<Integer, ArrayList<Short>> map = StringUtil.dataPackedConvert2(dataArray, 1);

            ArrayList<Short> list = map.get(0);

            parseEcgData(list);


        } else if ((value[2] & 0xff) == 0x16) { //软件版本号
            int len = value[3] & 0xff;
            byte[] b = new byte[len];
            System.arraycopy(value, 4, b, 0, len);
            VersionInfoEvent event = new VersionInfoEvent();
            event.version = new String(b);
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DEVICE_VERSION, event));

        } else if ((value[2] & 0xff) == 0x20) {  //设置通道数

            BLEManager.getInstance().setChannelCommand((byte) 0x08);

        } else if ((value[2] & 0xff) == 0x30) {

        } else if ((value[2] & 0xff) == 0x40) {
            BLEManager.getInstance().setTimeCommand();

        } else if ((value[2] & 0xff) == 0x21) {//拓展指令
            int len = value[3] & 0xff;
            byte[] data = new byte[len];
            System.arraycopy(value, 4, data, 0, len);
            //手表血压的数据
            if (BLEManager.getInstance().calculateXorWatch(data) == data[data.length - 1]) {
                BPWatchDataParse.parseData(data);
                BLEManager.getInstance().bpWatchData = "";
            } else {
                String mData = HexUtil.formatHexString(data);
                BLEManager.getInstance().bpWatchData += mData;
                byte[] mBytes = HexUtil.hexStringToBytes(BLEManager.getInstance().bpWatchData);
                if (BLEManager.getInstance().calculateXorWatch(mBytes) == mBytes[mBytes.length - 1]) {
                    BPWatchDataParse.parseData(mBytes);
                    BLEManager.getInstance().bpWatchData = "";
                }
            }

        } else if ((value[2] & 0xff) == 0x22) {
            //0x00未连接  0x01已连接
            //0x00未连接  0x01已连接
            int status = value[4] & 0xff;
            if (status == 1) {
                int spo2 = value[5] & 0xff;
                if (spo2 > 0)
                    EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_SPO2, spo2));
                else
                    EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_SPO2_INVALID));
            } else {
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_SPO2_INVALID));
            }
        } else if ((value[2] & 0xff) == 0x23) { //陀螺仪数据

            GyroscopeInfoEvent event = new GyroscopeInfoEvent();

            short x = (short) (value[5] & 0xff | ((value[6] & 0xff) << 8));
            short y = (short) (value[7] & 0xff | ((value[8] & 0xff) << 8));
            short z = (short) (value[9] & 0xff | ((value[10] & 0xff) << 8));

            event.x = ((double) x / 10000);
            event.y = ((double) y / 10000);
            event.z = ((double) z / 10000);

            double a = Math.acos(Math.abs(event.y) / Math.sqrt(event.x * event.x + event.y * event.y + event.z * event.z));

            event.degrees = 180 / Math.PI * a;

            System.out.println(event);
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_GYROSCOPE, event));

        } else if ((value[2] & 0xff) == 0x24) {//大气压强
            int len = value[3] & 0xff;
            AtmosphericPressure ap = new AtmosphericPressure();
            if (len == 2) {
                ap.value = value[4] & 0xff | ((value[5] & 0xff) << 8);
            } else if (len == 4) {
                ap.value = value[4] & 0xff | ((value[5] & 0xff) << 8);
                ap.temp = value[6] & 0xff | ((value[7] & 0xff) << 8);
            } else if (len == 6) {
                ap.value = value[4] & 0xff | ((value[5] & 0xff) << 8);
                ap.temp = value[6] & 0xff | ((value[7] & 0xff) << 8);
                ap.humidity = value[8] & 0xff | ((value[9] & 0xff) << 8);
            }
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_ATMOSPHERIC_PRESSURE, ap));

        } else if ((value[2] & 0xff) == 0x26) { //aa aa 26 06 00 51 0a 01 7d 0a 0d
            int temperature = (value[5] & 0xff) | ((value[6] & 0xff) << 8);
            if (temperature == 0x8000) {
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_TEMPERATURE_INVALID));
            } else {
                EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_TEMPERATURE, temperature));
            }
        } else if ((value[2] & 0xff) == 0x27) {
//        二氧化碳指令 0x27
//        AA AA 27 06 11 11 22 22 33 33 XX
//        1111->二氧化碳值16位（两字节），小端模式，无效值0xffff.
//        2222->温度值16位（两字节），小端模式，无效值0x8000
//        3333->相对湿度值16位（两字节），小端模式，无效值0xffff.
            int value1 = value[4] & 0xff | ((value[5] & 0xff) << 8);
            int value2 = value[6] & 0xff | ((value[7] & 0xff) << 8);
            int value3 = value[8] & 0xff | ((value[9] & 0xff) << 8);

            CarbonDioxideEvent dioxideEvent = new CarbonDioxideEvent();

            if (value1 != 0xffff)
                dioxideEvent.value = value1;
            if (value2 != 0x8000)
                dioxideEvent.temp = value2;
            if (value3 != 0xffff)
                dioxideEvent.humidity = value3;

            System.out.println(dioxideEvent.toString());
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_TEMPERATURE_HUMIDITY, dioxideEvent));

        } else if ((value[2] & 0xff) == 0x2f) {
            //aa aa 2f 18 45 30 2e 30 30 30 30 30 30 30 00 00 4e 30 2e 30 30 30 30 30 30 30 00 00 3c
            byte[] latByte = new byte[12];
            byte[] lngByte = new byte[12];
            System.arraycopy(value, 4, latByte, 0, 12);
            System.arraycopy(value, 16, lngByte, 0, 12);
            LatLngEvent latLngEvent = new LatLngEvent();
            latLngEvent.lat = new String(latByte).trim();
            latLngEvent.lng = new String(lngByte).trim();
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_LATITUDE_LONGITUDE, latLngEvent));
            System.out.println(latLngEvent.toString());


        } else if ((value[2] & 0xff) == 0x50) {
            //holter设置后的回包  aa aa 50 03 00 01 0c 5e
            HolterSetEvent event = new HolterSetEvent();
            event.mode = value[4]; //00非holter模式 01 holter模式
            event.status = value[5]; //00无效 01 启动 02停止

            EventBus.getDefault().post(new MsgEvent<>(EventAction.EVENT_HOLTER_SET, event));

        } else if ((value[2] & 0xff) == 0x51) {
            //是否为holter模式 以及存卡时间
            HolterModeEvent event = new HolterModeEvent();
            event.mode = value[4]; //00非holter模式 01 holter模式
            event.hours = value[5];
            event.minutes = value[6];
            event.seconds = value[7];
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_HOLTER_MODE, event));

        } else if ((value[2] & 0xff) == 0x60) {
            BLEManager.getInstance().setInitFinish(true);
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_INIT_FINISH));
            if (BaseApp.isDebug)
                System.out.println("init finish ok......");

        } else if ((value[2] & 0xff) == 0x62) {
            //文件名称
            FileNameEvent event = new FileNameEvent();
            System.arraycopy(value, 4, event.fileName, 0, 8);
            boolean x = false;
            for (byte b : event.fileName) {
                if (b != 0) {
                    x = true;
                    break;
                }
            }
            if (x)
                event.name = new String(event.fileName);
            System.out.println(event.toString());
        } else if ((value[2] & 0xff) == 0x67) {//文件名称
            FileMessageEvent event = new FileMessageEvent();
            event.all = value[4];
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_FILE_MESSAGE, event));
        } else if ((value[2] & 0xff) == 0x70) {

        } else if ((value[2] & 0xff) == 0x0e) {
            //导联状态信息
            LeadInfoEvent event = new LeadInfoEvent();
            event.low = value[4];
            event.high = value[5];

            int raV = StringUtil.getBitVale(event.low, 0);
            int laV = StringUtil.getBitVale(event.low, 1);
            int llV = StringUtil.getBitVale(event.low, 2);
            int v1V = StringUtil.getBitVale(event.low, 3);
            int v2V = StringUtil.getBitVale(event.low, 4);
            int v3V = StringUtil.getBitVale(event.low, 5);
            int v4V = StringUtil.getBitVale(event.low, 6);
            int v5V = StringUtil.getBitVale(event.low, 7);
            int v6V = StringUtil.getBitVale(event.high, 0);
            int rlV = StringUtil.getBitVale(event.high, 1);

            event.raV = raV;
            event.laV = laV;
            event.llV = llV;
            event.v1V = v1V;
            event.v2V = v2V;
            event.v3V = v3V;
            event.v4V = v4V;
            event.v5V = v5V;
            event.v6V = v6V;
            event.rlV = rlV;

            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_LEAD_STATE, event));

        } else if ((value[2] & 0xff) == 0x0f) {
            //心率和电量数据
            int hr = (value[4] & 0xff) + ((value[5] & 0xff) << 8);
            int battery = (value[6] & 0xff);

            HrBatteryEvent hrBattery = new HrBatteryEvent();
            hrBattery.hr = hr;
            hrBattery.battery = battery;
            System.out.println("hr is " + hr + ",battery is " + battery);

            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_HR_BATTERY, hrBattery));

            int len = value[3] & 0xff;

            if (len == 7) {
                BLEManager.getInstance().setLocalCount(false);
                int pr = value[9] & 0xff | ((value[10] & 0xff) << 8);
                int resp = value[9] | (value[10] << 8);
                System.out.println("pr == " + pr + ",resp == " + resp);
                if (resp != -100) {
                    RespInfoEvent event = new RespInfoEvent();
                    event.resp = resp;
                    EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_RESP_DATA, event));
                }
            }

        } else if ((value[2] & 0xff) == 0x1f) { //一键报警
            EventBus.getDefault().post(new MsgEvent<>(EventAction.DEVICE_WARN));

        } else if ((value[2] & 0xff) == 0xff) {
            //充电状态
            ChargeInfoEvent event = new ChargeInfoEvent();
            event.isCharging = (value[4] & 0xff) == 0x01;
            System.out.println(event.toString());
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_CHARGE_INFO, event));

        }
    }


    private void parseEcgData(ArrayList<Short> list) {
        ArrayList<Short> listResp = new ArrayList<>();
        ArrayList<Short> listLeadI = new ArrayList<>();
        ArrayList<Short> listLeadII = new ArrayList<>();
        ArrayList<Short> listLeadIII = new ArrayList<>();
        ArrayList<Short> listLeadV1 = new ArrayList<>();
        ArrayList<Short> listLeadV2 = new ArrayList<>();
        ArrayList<Short> listLeadV3 = new ArrayList<>();
        ArrayList<Short> listLeadV4 = new ArrayList<>();
        ArrayList<Short> listLeadV5 = new ArrayList<>();
        ArrayList<Short> listLeadV6 = new ArrayList<>();
        ArrayList<Short> listLeadAvr = new ArrayList<>();
        ArrayList<Short> listLeadAvl = new ArrayList<>();
        ArrayList<Short> listLeadAvf = new ArrayList<>();

        //resp,I, II，V1, V3, V4, V5, V6
        //III = II-I    aVR =  -0.5*(I+II)   aVL = I - 0.5*II   aVF = II - 0.5*I

        short leadI = 0;
        short leadII = 0;

        if (BLEManager.DEVICE_BENELIFE_U.equalsIgnoreCase(deviceName)) {
            //解析八个导联的数据
            for (int i = 0; i < list.size(); i++) {
                short data = list.get(i);
                if (i % 8 == 0) {
                    listResp.add(data);
                } else if (i % 8 == 1) {
                    leadI = data;
                    listLeadI.add(leadI);
                } else if (i % 8 == 2) {
                    leadII = data;
                    listLeadII.add(leadII);
                    short leadIII = (short) (leadII - leadI);
                    short leadAVR = (short) -((leadI + leadII) / 2);
                    short leadAVL = (short) (leadI - leadII / 2);
                    short leadAVF = (short) (leadII - leadI / 2);
                    listLeadIII.add(leadIII);
                    listLeadAvr.add(leadAVR);
                    listLeadAvl.add(leadAVL);
                    listLeadAvf.add(leadAVF);
                } else if (i % 8 == 3) {
                    listLeadV1.add(data);
                    short leadV2 = (short) (data * 1.544 - 0.28);
                    listLeadV2.add(leadV2);
                } else if (i % 8 == 4) {
                    listLeadV3.add(data);
                } else if (i % 8 == 5) {
                    listLeadV4.add(data);
                } else if (i % 8 == 6) {
                    listLeadV5.add(data);
                } else if (i % 8 == 7) {
                    listLeadV6.add(data);
                }
            }
            if (BLEManager.getInstance().isLocalCount()) {
                int size = BLEManager.getInstance().respData.size();
                if (size >= 4500) {
                    for (int i = 0; i < 1500; i++) {
                        BLEManager.getInstance().respData.removeFirst();
                    }
                }
                BLEManager.getInstance().respData.addAll(listResp);
            } else {
                BLEManager.getInstance().respData.clear();
            }
            Map<Integer, ArrayList<Short>> mapEcgData = new HashMap<>();

            byte[] xLeadDictionary = new byte[]{EcgDataContext.LEAD_I, EcgDataContext.LEAD_II, EcgDataContext.LEAD_III,
                    EcgDataContext.LEAD_aVR, EcgDataContext.LEAD_aVL, EcgDataContext.LEAD_aVF,
                    EcgDataContext.LEAD_V1, EcgDataContext.LEAD_V2, EcgDataContext.LEAD_V3,
                    EcgDataContext.LEAD_V4, EcgDataContext.LEAD_V5, EcgDataContext.LEAD_V6};

            mapEcgData.put(EcgDataContext.LEAD_I, listLeadI);
            mapEcgData.put(EcgDataContext.LEAD_II, listLeadII);
            mapEcgData.put(EcgDataContext.LEAD_III, listLeadIII);
            mapEcgData.put(EcgDataContext.LEAD_aVR, listLeadAvr);
            mapEcgData.put(EcgDataContext.LEAD_aVL, listLeadAvl);
            mapEcgData.put(EcgDataContext.LEAD_aVF, listLeadAvf);
            mapEcgData.put(EcgDataContext.LEAD_V1, listLeadV1);
            mapEcgData.put(EcgDataContext.LEAD_V2, listResp);
            mapEcgData.put(EcgDataContext.LEAD_V3, listLeadV3);
            mapEcgData.put(EcgDataContext.LEAD_V4, listLeadV4);
            mapEcgData.put(EcgDataContext.LEAD_V5, listLeadV5);
            mapEcgData.put(EcgDataContext.LEAD_V6, listLeadV6);

            mapEcgData = BLEManager.test.filter(mapEcgData, xLeadDictionary);

            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_I, mapEcgData.get(EcgDataContext.LEAD_I)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_II, mapEcgData.get(EcgDataContext.LEAD_II)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_III, mapEcgData.get(EcgDataContext.LEAD_III)));

            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVR, mapEcgData.get(EcgDataContext.LEAD_aVR)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVL, mapEcgData.get(EcgDataContext.LEAD_aVL)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVF, mapEcgData.get(EcgDataContext.LEAD_aVF)));

            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V1, mapEcgData.get(EcgDataContext.LEAD_V1)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_RESP, mapEcgData.get(EcgDataContext.LEAD_V2)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V3, mapEcgData.get(EcgDataContext.LEAD_V3)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V4, mapEcgData.get(EcgDataContext.LEAD_V4)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V5, mapEcgData.get(EcgDataContext.LEAD_V5)));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V6, mapEcgData.get(EcgDataContext.LEAD_V6)));

            listLeadV2.clear();
            List<Short> mList = mapEcgData.get(EcgDataContext.LEAD_V1);
            for (int i = 0; i < mList.size(); i++) {
                short leadV2 = (short) (mList.get(i) * 1.544 - 0.28);
                listLeadV2.add(leadV2);
            }
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V2, listLeadV2));

        } else {
            for (int i = 0; i < list.size(); i++) {
                short data = list.get(i);
                if (i % 8 == 0) {
                    leadI = data;
                    listLeadI.add(leadI);
                } else if (i % 8 == 1) {
                    leadII = data;
                    listLeadII.add(leadII);
                    short leadIII = (short) (leadII - leadI);
                    short leadAVR = (short) -((leadI + leadII) / 2);
                    short leadAVL = (short) (leadI - leadII / 2);
                    short leadAVF = (short) (leadII - leadI / 2);
                    listLeadIII.add(leadIII);
                    listLeadAvr.add(leadAVR);
                    listLeadAvl.add(leadAVL);
                    listLeadAvf.add(leadAVF);
                } else if (i % 8 == 2) {
                    listLeadV1.add(data);
                } else if (i % 8 == 3) {
                    listLeadV2.add(data);
                } else if (i % 8 == 4) {
                    listLeadV3.add(data);
                } else if (i % 8 == 5) {
                    listLeadV4.add(data);
                } else if (i % 8 == 6) {
                    listLeadV5.add(data);
                } else if (i % 8 == 7) {
                    listLeadV6.add(data);
                }
            }

            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_I, listLeadI));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_II, listLeadII));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_III, listLeadIII));

            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVR, listLeadAvr));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVL, listLeadAvl));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_AVF, listLeadAvf));

            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V1, listLeadV1));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V2, listLeadV2));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V3, listLeadV3));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V4, listLeadV4));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V5, listLeadV5));
            EventBus.getDefault().post(new MsgEvent<>(EventAction.ECG_DATA_LEAD_V6, listLeadV6));
        }
    }

}
